package com.zealcomm.ivcscustomer.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.zco.util.StringUtil
import com.zco.util.ToastUtil
import com.zco.util.listener.CustomOnClickListener
import com.zealcomm.commonui.base.BaseFragment
import com.zealcomm.ivcscustomer.R
import com.zealcomm.ivcscustomer.databinding.FragmentCustomerBaseInfoBinding
import com.zealcomm.ivcscustomer.databinding.ItemSimpleListBinding
import com.zealcomm.ivcscustomer.service.MainActivity
import com.zealcomm.utils.EditTextInputUtil
import kotlinx.coroutines.*

/**
 * 提交用户基本信息
 */
class CustomerBaseInfoFragment : BaseFragment() {

    private var binding: FragmentCustomerBaseInfoBinding? = null
    private var job: Job? = null
    private var productCategories = arrayListOf("个人贷款", "企业贷款", "小额贷款")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentCustomerBaseInfoBinding.inflate(inflater, container, false)
        binding?.rvProductCategories?.adapter = object : RecyclerView.Adapter<MyViewHolder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
                return object : MyViewHolder(
                    ItemSimpleListBinding.inflate(
                        layoutInflater,
                        parent,
                        false
                    ).root
                ) {}
            }

            override fun onBindViewHolder(
                holder: MyViewHolder,
                @SuppressLint("RecyclerView") position: Int
            ) {
                (holder.itemView as TextView).text = productCategories[position]
                holder.itemView.setOnClickListener(object : CustomOnClickListener<Any>() {
                    override fun onCustomClick(view: View?) {
                        binding?.tvProductCategories?.text = productCategories[position]
                        binding?.rvProductCategories?.visibility = View.GONE
                    }
                })
            }


            override fun getItemCount(): Int {
                return productCategories.size
            }

        }
        return binding?.root
    }

    private open class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding?.btnNext?.setOnClickListener(object : CustomOnClickListener<Any>() {
            override fun onCustomClick(view: View?) {
                if (TextUtils.isEmpty(binding?.etName?.text)) {
                    ToastUtil.showShort("请填写姓名")
                    return
                }
                if (TextUtils.isEmpty(binding?.etTelephone?.text)) {
                    ToastUtil.showShort("请填写手机号")
                    return
                }
                if (TextUtils.isEmpty(binding?.etIdCard?.text)) {
                    ToastUtil.showShort("请填写身份证号")
                    return
                }else if(!StringUtil.isIDCard(binding?.etIdCard?.text.toString())){
                    ToastUtil.showShort("身份证号码格式不正确")
                    return
                }
                if (TextUtils.isEmpty(binding?.tvProductCategories?.text)) {
                    ToastUtil.showShort("请选择产品品类")
                    return
                }
                (holdingActivity as MainActivity).submitBaseInfoFragment?.showVerifySuccess(true)
                job = GlobalScope.launch {
                    delay(800)
                    withContext(Dispatchers.Main) {
                        (holdingActivity as MainActivity).idCardAndBankInfo?.let {
                            it.ID = binding?.etIdCard?.text.toString()
                            it.name = binding?.etName?.text.toString()
                            it.productCategory = binding?.tvProductCategories?.text.toString()
                            it.mobile = binding?.etTelephone?.text.toString()
                        }
//                        (holdingActivity as SubmitFragmentInfoActivity).showVerifySuccess(false)
//                        (holdingActivity as SubmitFragmentInfoActivity)?.addFragment(
//                            IDCardAndBankFragment(),
//                            1
//                        )
                        (holdingActivity as MainActivity).submitBaseInfoFragment?.showVerifySuccess(
                            false
                        )
                        (holdingActivity as MainActivity).submitBaseInfoFragment?.addFragment(
                            IDCardAndBankFragment(),
                            1
                        )
                    }
                }
            }
        })
        binding?.tvProductCategories?.setOnClickListener(object : CustomOnClickListener<Any>() {
            override fun onCustomClick(view: View?) {
                if (binding?.rvProductCategories?.visibility == View.VISIBLE) {
                    binding?.rvProductCategories?.visibility = View.GONE
                } else {
                    binding?.rvProductCategories?.visibility = View.VISIBLE
                }
            }
        })
        binding?.etName?.let {
            EditTextInputUtil.setNameFilter(it)
        }
        binding?.etIdCard?.let {
            EditTextInputUtil.setIDCardFilter(it)
        }
    }

    override fun initView(view: View?, savedInstanceState: Bundle?) {
    }

    override val layoutId: Int
        get() = R.layout.fragment_customer_base_info

    override fun onDestroyView() {
        super.onDestroyView()
        binding = null
        (holdingActivity as MainActivity)?.submitBaseInfoFragment?.showVerifySuccess(false)
        job?.cancel()
    }

}